package red.lixiang.service.impl;

import red.lixiang.dao.CartDAO;
import red.lixiang.model.BaseInfo;
import red.lixiang.model.Cart;
import red.lixiang.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDAO cartDAO;

    @Override
    public BaseInfo addToCartByOpenId(String barCode, String openId) {
        cartDAO.addToCartByOpenId(barCode, openId);
        return new BaseInfo("0","OK");
    }

    @Override
    public BaseInfo deleteCartByOpenId(String barCode, String openId) {
        cartDAO.deleteCartByOpenId(barCode,openId);
        return new BaseInfo("0","OK");
    }

    @Override
    public BaseInfo getCartInfoByOpenId(String openId) {
        BaseInfo baseInfo = new BaseInfo();
        HashMap<String , Object> map = new HashMap<>();
        List<Cart> cartList =cartDAO.getCartInfoByOpenId(openId);
        int totalNum = cartList.size();
        map.put("totalNum",totalNum);
        map.put("products",cartList);
        baseInfo.setObject(map);
        return baseInfo;
    }
}
